package com.iot08.api.utils;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author cwj
 * @description
 * @date 2025/1/3 9:12
 **/
public class AESUtil {

    // AES 加密
    public static String encrypt(String data, String key) throws Exception {
        // 创建 AES 密钥
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");

        // 获取加密算法实例
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        // 初始化加密器
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // 执行加密
        byte[] encryptedBytes = cipher.doFinal(data.getBytes());

        // 返回加密后的数据（Base64 编码）
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    // AES 解密
    public static String decrypt(String encryptedData, String key) throws Exception {
        // 创建 AES 密钥
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");

        // 获取加密算法实例
        //Cipher cipher = Cipher.getInstance("AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        // 初始化解密器
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        // 解码 Base64 数据
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);

        // 执行解密
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        // 返回解密后的数据
        return new String(decryptedBytes);
    }


    /**
     * @description 解密微信支付回调
     * @author cwj
     * @param [associatedData, nonce, ciphertext, apiV3Key]
     * @return java.lang.String
     * @date @time 2025/1/4 11:08
     **/
    public static String decryptResource(String associatedData, String nonce, String ciphertext, String apiV3Key) throws Exception {
        // 转换 APIv3 密钥
        SecretKeySpec keySpec = new SecretKeySpec(apiV3Key.getBytes(StandardCharsets.UTF_8), "AES");

        // 初始化 Cipher
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec spec = new GCMParameterSpec(128, nonce.getBytes(StandardCharsets.UTF_8));
        cipher.init(Cipher.DECRYPT_MODE, keySpec, spec);
        if(StringUtils.isNotEmpty(associatedData)){
            // 设置附加数据
            cipher.updateAAD(associatedData.getBytes(StandardCharsets.UTF_8));
        }
        // 解密
        byte[] decodedCiphertext = Base64.getDecoder().decode(ciphertext);
        byte[] plaintext = cipher.doFinal(decodedCiphertext);
        return new String(plaintext, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        try {
            String data = "1703247844";
            String key = "pinxuan2024abcde"; // 16 字节的密钥，长度为 16 字节或 128 位

            // 加密
            String encryptedData = encrypt(data, key);
            System.out.println("Encrypted Data: " + encryptedData);

            //解密
            String decryptedData = decrypt(encryptedData, key);
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}